/**
 * @Project: InteractAPI
 * @Title: Player.java
 * @Package com.j99view.interact
 * @Description: TODO
 * Copyright: Copyright (c) 2010 
 * Company:ENDER.crop
 * 
 * @author ender
 * @date 2013-8-15 11:33:51
 * @version V1.0.0
 */
package com.j99view.interact;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.j99view.interact.action.InteractAction;
import com.j99view.interact.utils.SocketFactory;

/**
 * @ClassName: Player
 * @Description: TODO
 * @author ender
 * @date 2013-8-15 11:33:51
 *
 */
public class Player {
	
	private PlayerType playerType;
	
	private String version;
	
	private String ip = "";
	
	private int port = 0;
	
	private Socket socket;
	
	private boolean inited = false;

	/**
	 * @return the playerType
	 */
	public PlayerType getPlayerType() {
		return playerType;
	}

	/**
	 * @param playerType the playerType to set
	 */
	public void setPlayerType(PlayerType playerType) {
		this.playerType = playerType;
	}

	/**
	 * @return the version
	 */
	public String getVersion() {
		return version;
	}

	/**
	 * @param version the version to set
	 */
	public void setVersion(String version) {
		this.version = version;
	}

	/**
	 * @return the ip
	 */
	public String getIp() {
		return ip;
	}

	/**
	 * @param ip the ip to set
	 * @throws Exception 
	 */
	public void setIp(String ip) throws Exception {
		String ipRegex="(2[5][0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})";
		Pattern pattern = Pattern.compile(ipRegex);
		Matcher matcher = pattern.matcher(ip);
		if(matcher.matches()){
			this.ip = ip;
			this.inited = false;
		}else{
			throw new Exception("Ip adress is not illegle");
		}
		
	}

	/**
	 * @return the port
	 */
	public int getPort() {
		return port;
	}

	/**
	 * @param port the port to set
	 */
	public void setPort(int port) throws Exception{
		if(port > 0 && port < 65536){
			this.port = port;
			this.inited = false;
		}else{
			throw new Exception("Port is not illegle which should be between 1 to 65535");
		}
		
	}
	
	public void init() throws Exception{
		if(ip == ""){
			throw new Exception("Player must get a IP adress like 127.0.0.1");
		}
		
		if(port == 0){
			throw new Exception("Player must have a port like 5527");
		}
		
		socket = SocketFactory.getSocket(this);
		this.inited = true;
	}
	
	
	public void executeAction(InteractAction interactAction) throws Exception{
		//TODO 用player的ip和prot  在ConnectionFactory里得到socket 再调用interAction中的抽象方法将数据写入网络
		if(this.inited){
			interactAction.write(socket);
		}else{
			throw new Exception("Player is not init yet or some params changed");
		}
		
	}
	
	public void closeSocket(){
		if(this.socket!=null){
			try {
				this.socket.close();
				this.inited = false;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	public boolean isOnline(){
		
		return true;
	}
	
}


enum PlayerType{
	SIGMA,EPW
}